﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Web;
using System.Drawing.Imaging;
using System.Drawing;

namespace QidorSpider.Components.Util
{
    public class Common
    {
        /// <summary>
        /// class type
        /// </summary>
        public enum ClassType
        {
            Plan,
            Key,
            Post,
        }

        /// <summary>
        /// post type
        /// </summary>
        public enum PostType
        {
            NoPost,
            PostAccess,
            PostMsSql,
            PostText,
            PostExcel,
            PostMysql,
            PostWeb,
        }

        #region 枚举类型
        public enum TaskState
        {
            UnStart = 1010,
            Started = 1011,
            Aborted = 1012,
            Waiting = 1013,
            Running = 1014,
            Pause = 1015,
            Stopped = 1016,
            Exporting = 1017,
            Completed = 1018,
            Failed = 1019,
            Publishing = 1020,
        }

        public enum GatherResult
        {
            GatherSucceed = 1071,
            GatherFailed = 1072,
            PublishSuccees = 1073,
            PublishFailed = 1074,
        }

        public enum DownloadResult
        {
            Succeed = 1081,
            Failed = 1082,
            Err = 1083,
        }

        public enum GDataType
        {
            Txt = 1091,
            Picture = 1092,
            Flash = 1093,
            File = 1094,
        }

        public enum GatherThreadState
        {
            UnStart = 1030,
            Started = 1031,
            Running = 1032,
            Stopped = 1033,
            Completed = 1034,
            Failed = 1035,
            Aborted = 1036,
            Waiting = 1037,
        }

        public enum FormState
        {
            New = 1021,
            Edit = 1022,
            Browser = 1023,
        }

        public enum TaskRunType
        {
            OnlyGather = 1041,
            GatherExportData = 1042,
        }

        public enum TaskType
        {
            HtmlByUrl = 1051,
            RssByUrl = 1052,
            HtmlByWeb = 1053,
            AjaxHtmlByUrl = 1054,
        }

        public enum PublishType
        {
            NoPublish = 1060,
            PublishAccess = 1061,
            PublishMSSql = 1062,
            PublishTxt = 1063,
            PublishExcel = 1064,
            PublishMySql = 1065,
            PublishWeb = 1066,
        }

        public enum LimitSign
        {
            NoLimit = 2001,          //ｲｻﾗﾎﾒ篋ｽｵﾄﾏﾞﾖﾆ
            NoWebSign = 2002,        //ﾆ･ﾅ萍ｱﾈ･ｵｳｷ釚ﾅ
            OnlyCN = 2003,           //ﾖｻﾆ･ﾅ葷ﾐﾎﾄ
            OnlyDoubleByte = 2004,     //ﾖｻﾆ･ﾅ萢ｫﾗﾖｽﾚﾗﾖｷ・
            OnlyNumber = 2005,         //ﾖｻﾆ･ﾅ萍ﾗﾖ
            OnlyChar = 2006,           //ﾖｻﾆ･ﾅ葫ﾖﾄｸﾊﾗﾖｼｰｳ｣ﾓﾃﾗﾖｷ・
            Custom = 2007,           //ﾗﾔｶｨﾒ袗ﾔ･ﾅ莖昻・ｽ 
        }

        public enum ExportLimit
        {
            ExportNoLimit = 2040,      //ｲｻﾗ莎ﾘﾖﾆ
            ExportNoWebSign = 2041,    //ﾊ莎ｱﾈ･ｵｳｷ釚ﾅ
            ExportPrefix = 2042,       //ﾊ莎ｱｸｽｼﾓﾇｰﾗｺ  
            ExportSuffix = 2043,       //ﾊ莎ｱｸｽｼﾓｺｺ
            ExportTrimLeft = 2044,     //ﾗ･ｵﾖｷ・
            ExportTrimRight = 2045,    //ﾓﾒﾆ･ｵﾖｷ・
            ExportReplace = 2046,      //ﾌ貊ｻﾆ葷ﾐｷ釚ﾏﾌｵﾄﾗﾖｷ・
            ExportTrim = 2047,         //ﾈ･ｵﾖｷ逸ｮﾊﾗﾎｲｿﾕｸ・
            ExportRegexReplace = 2048, //ﾊ莎ｱｲﾉﾓﾃﾕﾔ昻・ｽｽﾐﾌ貊ｻ


        }

        public enum WebCode
        {
            auto = 1000,
            gb2312 = 1001,
            utf8 = 1002,
            gbk = 1003,
            big5 = 1004,
            NoCoding = 1005,
        }

        public enum ExitPara
        {
            Exit = 2010,
            MinForm = 2012,
        }

        public enum UpdateUrlCountType
        {
            Gathered = 2020,
            Err = 2021,
            ReIni = 2022,
            UrlCountAdd = 2023,
            ErrUrlCountAdd = 2024,
        }

        public enum UrlGatherResult
        {
            UnGather = 2031,
            Succeed = 2032,
            Error = 2033,
            Gathered = 2034,
        }

        public enum DatabaseType
        {
            Access = 2051,
            MSSqlServer = 2052,
            MySql = 2053,
        }

        public enum LogType
        {
            Info = 2061,
            Error = 2062,
            Warning = 2063,
            RunPlanTask = 2064,
            GatherError = 2065,
            PublishError = 2066,
        }

        public enum RunTaskType
        {
            SoukeyTask = 2071,           //Soukeyｲﾉﾕｪﾈﾎﾎ・
            DataTask = 2072,               //ﾊｾﾝｿ箒豢｢ｹｳﾌ
            OtherTask = 2073,            //ﾍ篆ﾓｿﾉﾖｴﾐﾐﾃ・・
        }

        public enum RunTaskPlanType
        {
            Ones = 2081,                //ｽﾋﾐﾐﾒｻｴﾎ
            DayOnes = 2082,             //ﾃｿﾌ・ﾋﾐﾐﾒｻｴﾎ
            DayTwice = 2083,            //ﾃｿﾌ・ﾏﾎ醯ﾂﾎ邵ﾋﾐﾐﾒｻｴﾎ
            Weekly = 2084,              //ﾃｿﾖﾜﾔﾋﾐﾐ
            Custom = 2085,              //ﾗﾔｶｨﾒ袂ﾋﾐﾐｼ荳・
        }

        public enum PlanDisabledType
        {
            RunTime = 2091,            //ｰｴﾕﾕﾔﾋﾐﾐｴﾎﾊﾊｧﾐｧ
            RunDateTime = 2092,        //ｰｴﾕﾕﾊｱｼ萍ｧﾐｧ 
        }

        public enum PlanState
        {
            Enabled = 3001,           //ﾓﾐﾐｧ
            Disabled = 3002,          //ﾎﾞﾐｧ
            Expired = 3003,          //ｹﾆﾚ
        }

        //ｼ猯ﾀ獎ﾍ
        public enum MessageType
        {
            RunSoukeyTask = 3010,
            RunFileTask = 3011,
            RunData = 3013,
            ReloadPlan = 3013,
            MonitorFileFaild = 3014,
        }

        //ｴ･ｷ｢ﾆ獎ﾍ
        public enum TriggerType
        {
            GatheredRun = 3020,
            PublishedRun = 3021,
        }

        public enum CurLanguage
        {
            Auto = 3031,
            zhCN = 3032,
            enUS = 3033,
        }

        #endregion

        #region 程序根目录
        /// <summary>
        /// 程序根目录
        /// </summary>
        public static string ApplicationPath
        {
            get
            {
                return Application.StartupPath + "\\";
            }
        }
        #endregion

        #region 获得当前绝对路径
        /// <summary>
        /// 获得当前绝对路径
        /// </summary>
        /// <param name="strPath">指定的路径</param>
        /// <returns>绝对路径</returns>
        public static string GetMapPath(string strPath)
        {
            if(HttpContext.Current != null)
            {
                return HttpContext.Current.Server.MapPath(strPath);
            }
            else //非web程序引用
            {
                strPath = strPath.Replace("/", "\\");
                if(strPath.StartsWith("\\"))
                {
                    strPath = strPath.Substring(strPath.IndexOf('\\', 1)).TrimStart('\\');
                }
                return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
            }
        }
        #endregion

        #region 加图片水印
        /// <summary>
        /// 加图片水印
        /// </summary>
        /// <param name="filename">文件名</param>
        /// <param name="watermarkFilename">水印文件名</param>
        /// <param name="watermarkStatus">图片水印位置</param>
        public static void AddImageSignPic(Image img, string filename, string watermarkFilename, int watermarkStatus, int quality, int watermarkTransparency)
        {
            Graphics g = Graphics.FromImage(img);
            //设置高质量插值法
            //g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            //设置高质量,低速度呈现平滑程度
            //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            Image watermark = new Bitmap(watermarkFilename);

            if(watermark.Height >= img.Height || watermark.Width >= img.Width)
                return;

            ImageAttributes imageAttributes = new ImageAttributes();
            ColorMap colorMap = new ColorMap();

            colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
            colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
            ColorMap[] remapTable = { colorMap };

            imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);

            float transparency = 0.5F;
            if(watermarkTransparency >= 1 && watermarkTransparency <= 10)
                transparency = (watermarkTransparency / 10.0F);


            float[][] colorMatrixElements = {
												new float[] {1.0f,  0.0f,  0.0f,  0.0f, 0.0f},
												new float[] {0.0f,  1.0f,  0.0f,  0.0f, 0.0f},
												new float[] {0.0f,  0.0f,  1.0f,  0.0f, 0.0f},
												new float[] {0.0f,  0.0f,  0.0f,  transparency, 0.0f},
												new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}
											};

            ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);

            imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

            int xpos = 0;
            int ypos = 0;

            switch(watermarkStatus)
            {
                case 1:
                    xpos = (int)(img.Width * (float).01);
                    ypos = (int)(img.Height * (float).01);
                    break;
                case 2:
                    xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
                    ypos = (int)(img.Height * (float).01);
                    break;
                case 3:
                    xpos = (int)((img.Width * (float).99) - (watermark.Width));
                    ypos = (int)(img.Height * (float).01);
                    break;
                case 4:
                    xpos = (int)(img.Width * (float).01);
                    ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
                    break;
                case 5:
                    xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
                    ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
                    break;
                case 6:
                    xpos = (int)((img.Width * (float).99) - (watermark.Width));
                    ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
                    break;
                case 7:
                    xpos = (int)(img.Width * (float).01);
                    ypos = (int)((img.Height * (float).99) - watermark.Height);
                    break;
                case 8:
                    xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
                    ypos = (int)((img.Height * (float).99) - watermark.Height);
                    break;
                case 9:
                    xpos = (int)((img.Width * (float).99) - (watermark.Width));
                    ypos = (int)((img.Height * (float).99) - watermark.Height);
                    break;
            }

            g.DrawImage(watermark, new Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);

            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
            ImageCodecInfo ici = null;
            foreach(ImageCodecInfo codec in codecs)
            {
                if(codec.MimeType.IndexOf("jpeg") > -1)
                    ici = codec;
            }
            EncoderParameters encoderParams = new EncoderParameters();
            long[] qualityParam = new long[1];
            if(quality < 0 || quality > 100)
                quality = 80;

            qualityParam[0] = quality;

            EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam);
            encoderParams.Param[0] = encoderParam;

            if(ici != null)
                img.Save(filename, ici, encoderParams);
            else
                img.Save(filename);

            g.Dispose();
            img.Dispose();
            watermark.Dispose();
            imageAttributes.Dispose();
        }
        #endregion

        #region 增加图片文字水印
        /// <summary>
        /// 增加图片文字水印
        /// </summary>
        /// <param name="filename">文件名</param>
        /// <param name="watermarkText">水印文字</param>
        /// <param name="watermarkStatus">图片水印位置</param>
        public static void AddImageSignText(Image img, string filename, string watermarkText, int watermarkStatus, int quality, string fontname, int fontsize)
        {
            Graphics g = Graphics.FromImage(img);
            Font drawFont = new Font(fontname, fontsize, FontStyle.Regular, GraphicsUnit.Pixel);
            SizeF crSize;
            crSize = g.MeasureString(watermarkText, drawFont);

            float xpos = 0;
            float ypos = 0;

            switch(watermarkStatus)
            {
                case 1:
                    xpos = (float)img.Width * (float).01;
                    ypos = (float)img.Height * (float).01;
                    break;
                case 2:
                    xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
                    ypos = (float)img.Height * (float).01;
                    break;
                case 3:
                    xpos = ((float)img.Width * (float).99) - crSize.Width;
                    ypos = (float)img.Height * (float).01;
                    break;
                case 4:
                    xpos = (float)img.Width * (float).01;
                    ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
                    break;
                case 5:
                    xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
                    ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
                    break;
                case 6:
                    xpos = ((float)img.Width * (float).99) - crSize.Width;
                    ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
                    break;
                case 7:
                    xpos = (float)img.Width * (float).01;
                    ypos = ((float)img.Height * (float).99) - crSize.Height;
                    break;
                case 8:
                    xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
                    ypos = ((float)img.Height * (float).99) - crSize.Height;
                    break;
                case 9:
                    xpos = ((float)img.Width * (float).99) - crSize.Width;
                    ypos = ((float)img.Height * (float).99) - crSize.Height;
                    break;
            }

            g.DrawString(watermarkText, drawFont, new SolidBrush(Color.White), xpos + 1, ypos + 1);
            g.DrawString(watermarkText, drawFont, new SolidBrush(Color.Black), xpos, ypos);

            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
            ImageCodecInfo ici = null;
            foreach(ImageCodecInfo codec in codecs)
            {
                if(codec.MimeType.IndexOf("jpeg") > -1)
                    ici = codec;
            }
            EncoderParameters encoderParams = new EncoderParameters();
            long[] qualityParam = new long[1];
            if(quality < 0 || quality > 100)
                quality = 80;

            qualityParam[0] = quality;

            EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam);
            encoderParams.Param[0] = encoderParam;

            if(ici != null)
                img.Save(filename, ici, encoderParams);
            else
                img.Save(filename);

            g.Dispose();
            img.Dispose();
        }
        #endregion



    }
}
